package com.cuixichang.minimalism.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseFieldNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.cuixichang.minimalism.mapper.ILoginRoleMapper;
import com.cuixichang.minimalism.transfer.dto.ILoginRoleDTO;
import com.cuixichang.minimalism.transfer.factory.ILoginRoleAssembler;
import com.cuixichang.minimalism.transfer.po.ILoginRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.function.Function;
import org.springframework.stereotype.Service;

/**
 * 账户与角色关系表表服务实现类
 *
 * @author cuixichang
 * @date 2024-12-18
 */
@Service
public class ILoginRoleServiceImpl implements ILoginRoleService {

    @Autowired
    private ILoginRoleMapper iLoginRoleMapper;

    @Override
    public ILoginRoleDTO selectById(Long id){
        ILoginRole iLoginRole = iLoginRoleMapper.selectById(id);
        return ILoginRoleAssembler.INSTANCT.convertDto(iLoginRole);
    }

    @Override
    public ILoginRoleDTO selectOne(ILoginRoleDTO iLoginRoleDTO){
        Assert.notNull(iLoginRoleDTO,"参数为空,请检查");
        ILoginRole iLoginRole = ILoginRoleAssembler.INSTANCT.convertPo(iLoginRoleDTO);
        ILoginRole result = iLoginRoleMapper.selectOne(new QueryWrapper<>(iLoginRole));
        return ILoginRoleAssembler.INSTANCT.convertDto(result);
    }

    @Override
    public List<ILoginRoleDTO> selectByIds(List<Long> ids) {
        Assert.notEmpty(ids,"参数为空,请检查");
        List<ILoginRole> results = iLoginRoleMapper.selectBatchIds(ids);
        return results.stream().map(ILoginRoleAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public List<ILoginRoleDTO> selectList(ILoginRoleDTO iLoginRoleDTO){
        return this.selectList(iLoginRoleDTO, (iLoginRole, queryWrapper) -> {});
    }

    @Override
    public List<ILoginRoleDTO> selectList(ILoginRoleDTO iLoginRoleDTO, BiConsumer<ILoginRole, QueryWrapper<ILoginRole>> consumer){
        Assert.notNull(iLoginRoleDTO,"参数为空,请检查");
        ILoginRole iLoginRole = ILoginRoleAssembler.INSTANCT.convertPo(iLoginRoleDTO);
        QueryWrapper<ILoginRole> queryWrapper = new QueryWrapper<>(iLoginRole);
        if(Objects.nonNull(consumer)){
            consumer.accept(iLoginRole, queryWrapper);
        }
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        List<ILoginRole> results = iLoginRoleMapper.selectList(queryWrapper);
        return results.stream().map(ILoginRoleAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public Page<ILoginRoleDTO> selectPage(ILoginRoleDTO iLoginRoleDTO,long pageSize,long pageNo){
        return this.selectPage(iLoginRoleDTO, pageSize, pageNo, (iLoginRole, queryWrapper) -> {});
    }

    @Override
    public Page<ILoginRoleDTO> selectPage(ILoginRoleDTO iLoginRoleDTO,long pageSize,long pageNo, BiConsumer<ILoginRole, QueryWrapper<ILoginRole>> consumer){
        Assert.notNull(iLoginRoleDTO,"参数为空,请检查");
        ILoginRole iLoginRole = ILoginRoleAssembler.INSTANCT.convertPo(iLoginRoleDTO);
        QueryWrapper<ILoginRole> queryWrapper = new QueryWrapper<>(iLoginRole);
        if(Objects.nonNull(consumer)) consumer.accept(iLoginRole, queryWrapper);
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        Page<ILoginRole> page = iLoginRoleMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper);
        Page<ILoginRoleDTO> result = Page.of(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords().stream()
                .map(ILoginRoleAssembler.INSTANCT::convertDto).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int deleteById(Long id){
        return iLoginRoleMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Long> ids){
        return iLoginRoleMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByCondition(ILoginRoleDTO iLoginRoleDTO){
        return deleteByCondition(iLoginRoleDTO, (iLoginRole, queryWrapper)-> {});
    }

    @Override
    public int deleteByCondition(ILoginRoleDTO iLoginRoleDTO, BiConsumer<ILoginRole, QueryWrapper<ILoginRole>> consumer){
        Assert.notNull(iLoginRoleDTO,"参数为空,请检查");
        ILoginRole iLoginRole = ILoginRoleAssembler.INSTANCT.convertPo(iLoginRoleDTO);
        QueryWrapper<ILoginRole> queryWrapper = new QueryWrapper<>(iLoginRole);
        if(Objects.nonNull(consumer)) {
            consumer.accept(iLoginRole, queryWrapper);
        }
        return iLoginRoleMapper.delete(queryWrapper);
    }

    @Override
    public ILoginRoleDTO insert(ILoginRoleDTO iLoginRoleDTO){
        return insertHasOption(iLoginRoleDTO, (ILoginRoleDTO) -> null);
    }

    @Override
    public ILoginRoleDTO insertHasOption(ILoginRoleDTO iLoginRoleDTO, Function<ILoginRoleDTO,ILoginRoleDTO> preOption){
        Assert.notNull(iLoginRoleDTO,"参数为空,请检查");
        iLoginRoleDTO.check();
        ILoginRole iLoginRole = ILoginRoleAssembler.INSTANCT.convertPo(iLoginRoleDTO);
        Long createLoginId = iLoginRole.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iLoginRole.setUpdateLoginId(createLoginId);
        LocalDateTime localDateTime = LocalDateTime.now();
        iLoginRole.setCreateTime(localDateTime);
        iLoginRole.setUpdateTime(localDateTime);
        Boolean isEnable = iLoginRole.getIsEnable();
        if(Objects.isNull(isEnable)) iLoginRole.setIsEnable(true);
        Integer sort = iLoginRole.getSort();
        if(Objects.isNull(sort)) iLoginRole.setSort(0);
        if(Objects.nonNull(preOption)){
            ILoginRoleDTO apply = preOption.apply(iLoginRoleDTO);
            if(Objects.nonNull(apply)) {
              return apply;
            }
        }
        int insert = iLoginRoleMapper.insert(iLoginRole);
        Assert.isTrue(insert>0,"插入失败");
        return ILoginRoleAssembler.INSTANCT.convertDto(iLoginRole);
    }

    @Override
    public int updateById(ILoginRoleDTO iLoginRoleDTO){
         return updateById(iLoginRoleDTO, (iLoginRole, updateWrapper) -> {});
    }

    @Override
    public int updateById(ILoginRoleDTO iLoginRoleDTO, BiConsumer<ILoginRole, UpdateWrapper<ILoginRole>> consumer){
        Assert.notNull(iLoginRoleDTO,"参数为空,请检查");
        Long id = iLoginRoleDTO.getId();
        Assert.notNull(id,"主键为空,请检查");
        ILoginRole iLoginRole = ILoginRoleAssembler.INSTANCT.convertPo(iLoginRoleDTO);
        Long createLoginId = iLoginRole.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iLoginRole.setCreateLoginId(null);
        iLoginRole.setUpdateLoginId(createLoginId);
        iLoginRole.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<ILoginRole> updateWrapper = new UpdateWrapper<ILoginRole>().eq(BaseFieldNames.ID, id);
        if(Objects.nonNull(consumer)) consumer.accept(iLoginRole,updateWrapper);
        return iLoginRoleMapper.update(iLoginRole,updateWrapper);
    }
}
